//index.js
//获取应用实例
var app = getApp()
// var util = require('../../utils/util.js')

var Util = require('../../utils/util.js');
var address = require('../../utils/city.js')
var address1 = require('../../utils/city.js')
var animation
var animation1
var that
var utilMd5 = require('../../utils/md5.js');

Page({
      data: {
            num: '',
            name: "",
            cph: "",
            phone_sj: "",
            showdate: false,
            dates: '',
            sfdi: "",
            disable: false,
            googinfo: "",
            goodsinfo:"",
            dun: "",
            fang: "",
            jian: "",
            zyf: "",
            freight:"",
            yfk: "",
            hdyj: "",
            mudidi: "",
            time: '',
            shaddress: '',
            fhaddress: '',
            menuType: 0,
            begin: null,
            status: 1,
            end: null,
            isVisible: false,
            animationData: {},
            animationAddressMenu: {},
            addressMenuIsShow: false,
            value: [0, 0, 0],
            provinces: [],
            citys: [],
            areas: [],
            province: '',
            city: '',
            area: '',

            menuType1: 0,
            begin1: null,
            status1: 1,
            end1: null,
            isVisible1: false,
            animationData1: {},
            animationAddressMenu1: {},
            addressMenuIsShow1: false,
            value1: [0, 0, 0],
            provinces1: [],
            citys1: [],
            areas1: [],
            areaInfo1: '',


            inputdis: false,
      },

      bindTimeChange: function (e) {
            console.log('picker发送选择改变，携带值为', e.detail.value)
            this.setData({
                  time: e.detail.value + ':00'
            })
      },
      // selectDistrict:function(e){
      //       that.setData({
      //             fhaddress: e.detail.value
      //       })
      // },
      //   点击所在地区弹出选择框
      selectDistrict: function (e) {
            var that = this;
            this.setData({
                  num: 0,
            })
            // 如果已经显示，不在执行显示动画
            if (that.data.addressMenuIsShow) {
                  return
            }
            // 执行显示动画
            that.startAddressAnimation(true)
      },
      selectDistrict1: function (e) {
            var that = this;
            this.setData({
                  num: 1,
            })
            // 如果已经显示，不在执行显示动画
            if (that.data.addressMenuIsShow) {
                  return
            }
            // 执行显示动画
            that.startAddressAnimation(true)
      },
      // 执行动画
      startAddressAnimation: function (isShow) {
            console.log(isShow)
            var that = this
            if (isShow) {
                  // vh是用来表示尺寸的单位，高度全屏是100vh
                  console.log(that.animation)
                  that.animation.translateY(0 + 'vh').step()
            } else {
                  that.animation.translateY(60 + 'vh').step()
            }
            that.setData({
                  animationAddressMenu: that.animation.export(),
                  addressMenuIsShow: isShow,
            })
      },
      startAddressAnimation1: function (isShow) {
            console.log(isShow)
            var that = this
            if (isShow) {
                  // vh是用来表示尺寸的单位，高度全屏是100vh
                  console.log(that.animation)
                  that.animation.translateY(0 + 'vh').step()
            } else {
                  that.animation.translateY(40 + 'vh').step()
            }
            that.setData({
                  animationAddressMenu1: that.animation.export(),
                  addressMenuIsShow1: isShow,
            })
      },
      // 点击地区选择取消按钮
      cityCancel: function (e) {
            this.startAddressAnimation(false)
      },

      // 点击地区选择确定按钮
      citySure: function (e) {

            var that = this
            var city = that.data.city
            var value = that.data.value;
            console.log(this.data.num + '我是num')
            that.startAddressAnimation(false)
            // 将选择的城市信息显示到输入框
            var areaInfo = that.data.provinces[value[0]].name + ' ' + that.data.citys[value[1]].name + ' ' + that.data.areas[value[2]].name;
            console.log(areaInfo)
            if (this.data.num == 0) {
                  this.setData({
                        fhaddress: areaInfo,
                  })
            } else if (this.data.num == 1) {
                  this.setData({
                        shaddress: areaInfo,

                  })
            }

      },
      citySure1: function (e) {

            var that = this
            var city = that.data.city
            var value = that.data.value
            that.startAddressAnimation(false)
            // 将选择的城市信息显示到输入框
            var areaInfo = that.data.provinces[value[0]].name + ' ' + that.data.citys[value[1]].name + ' ' + that.data.areas[value[2]].name;
            console.log('你点击我了t')
            this.setData({
                  shaddress: areaInfo,
            })
      },
      // 点击蒙版时取消组件的显示
      hideCitySelected: function (e) {
            console.log(e)
            this.startAddressAnimation(false)
      },

      // 处理省市县联动逻辑
      cityChange: function (e) {
            console.log(e)
            var value = e.detail.value
            var provinces = this.data.provinces
            var citys = this.data.citys
            var areas = this.data.areas
            var provinceNum = value[0]
            var cityNum = value[1]
            var countyNum = value[2]
            // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
            if (this.data.value[0] != provinceNum) {
                  var id = provinces[provinceNum].id
                  this.setData({
                        value: [provinceNum, 0, 0],
                        citys: address.citys[id],
                        areas: address.areas[address.citys[id][0].id],
                  })
            } else if (this.data.value[1] != cityNum) {
                  // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
                  var id = citys[cityNum].id
                  this.setData({
                        value: [provinceNum, cityNum, 0],
                        areas: address.areas[citys[cityNum].id],
                  })
            } else {
                  // 滑动选择了区
                  this.setData({
                        value: [provinceNum, cityNum, countyNum]
                  })
            }
            console.log(this.data)
      },
      cityChange1: function (e) {
            console.log(e)
            var value = e.detail.value
            var provinces = this.data.provinces
            var citys = this.data.citys
            var areas = this.data.areas
            var provinceNum = value[0]
            var cityNum = value[1]
            var countyNum = value[2]
            // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
            if (this.data.value[0] != provinceNum) {
                  var id = provinces[provinceNum].id
                  this.setData({
                        value: [provinceNum, 0, 0],
                        citys: address.citys[id],
                        areas: address.areas[address.citys[id][0].id],
                  })
            } else if (this.data.value[1] != cityNum) {
                  // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
                  var id = citys[cityNum].id
                  this.setData({
                        value: [provinceNum, cityNum, 0],
                        areas: address.areas[citys[cityNum].id],
                  })
            } else {
                  // 滑动选择了区
                  this.setData({
                        value: [provinceNum, cityNum, countyNum]
                  })
            }
            console.log(this.data)
      },
      tiaozhuan() {
            wx.navigateTo({
                  url: '../../pages/modelTest/modelTest',
            })
      },
      getendaddress: function (e) {
            that.setData({
                  sfdi: e.detail.value
            })
      },
      getgoodinfo: function (e) {
        console.log("监听货物名称信息为：------------" + e.detail.value);
        that.setData({
          goodsinfo: e.detail.value
        })
          if(e.detail.value.length>0){
           
          }
           
      },
      dun: function (e) {
            that.setData({
                  dun: e.detail.value
            })
      },
      fang: function (e) {
            that.setData({
                  fang: e.detail.value
            })
      },
      zyf: function (e) {
            that.setData({
                  freight: e.detail.value
            })
      },
      yfk: function (e) {
            that.setData({
                  yfk: e.detail.value
            })
      },
      hdyj: function (e) {
            that.setData({
                  hdyj: e.detail.value
            })
      },
      jian: function (e) {
            that.setData({
                  jian: e.detail.value
            })

      },
      getaddress: function (e) {
            that.setData({
                  mudidi: e.detail.value
            })
      },

      //   selectDistrict1: function (e) {
      //     var that = this
      //     // 如果已经显示，不在执行显示动画
      //     if (that.data.addressMenuIsShow1) {
      //       return
      //     }
      //     // 执行显示动画
      //     that.startAddressAnimation1(true)
      //   },
      // 执行动画
      //   startAddressAnimation1: function (isShow) {
      //     console.log(isShow+'--------------------f1')
      //     var that = this
      //     if (isShow) {

      //       // vh是用来表示尺寸的单位，高度全屏是100vh
      //       that.animation.translateY(0 + 'vh').step()
      //       console.log(this.animation)
      //     } else {
      //       that.animation.translateY(40 + 'vh').step()
      //     }
      //     that.setData({
      //       animationAddressMenu1: that.animation.export(),
      //       addressMenuIsShow1: isShow,
      //     })
      //   },
      // 点击地区选择取消按钮
      cityCancel1: function (e) {
            this.startAddressAnimation1(false)
      },
      // 点击地区选择确定按钮
      //   citySure1: function (e) {
      //     var that = this
      //     var city = that.data.city
      //     var value = that.data.value
      //     that.startAddressAnimation1(false)
      //     // 将选择的城市信息显示到输入框
      //     var areaInfo1 = that.data.provinces[value[0]].name + ' ' + that.data.citys[value[1]].name + ' ' + that.data.areas[value[2]].name;
      //     console.log("----ok-"+areaInfo1);

      //     this.setData({
      //           shaddress: areaInfo1,
      //     })
      //   },
      // 点击蒙版时取消组件的显示
      hideCitySelected1: function (e) {
            console.log(e)
            this.startAddressAnimation1(false)
      },

      // 处理省市县联动逻辑
      //   cityChange1: function (e) {
      //     console.log(e)
      //     var value = e.detail.value
      //     var provinces = this.data.provinces
      //     var citys = this.data.citys
      //     var areas = this.data.areas
      //     var provinceNum = value[0]
      //     var cityNum = value[1]
      //     var countyNum = value[2]
      //     // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
      //     if (this.data.value[0] != provinceNum) {
      //       var id = provinces[provinceNum].id
      //       this.setData({
      //         value: [provinceNum, 0, 0],
      //         citys: address.citys[id],
      //         areas: address.areas[address.citys[id][0].id],
      //       })
      //     } else if (this.data.value[1] != cityNum) {
      //       // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      //       var id = citys[cityNum].id
      //       this.setData({
      //         value: [provinceNum, cityNum, 0],
      //         areas: address.areas[citys[cityNum].id],
      //       })
      //     } else {
      //       // 滑动选择了区
      //       this.setData({
      //         value: [provinceNum, cityNum, countyNum]
      //       })
      //     }
      //     console.log(this.data)
      //   },











      onLoad: function (options) {
            // 默认联动显示北京
            // 初始化动画变量
            // var animation = wx.createAnimation({
            //   duration: 500,
            //   transformOrigin: "50% 50%",
            //   timingFunction: 'ease',
            // })
            console.log(options)

            // 初始化动画变量
            var animation = wx.createAnimation({
                  duration: 500,
                  transformOrigin: "50% 50%",
                  timingFunction: 'ease',
            })
            this.animation = animation;
            var animation1 = wx.createAnimation({
                  duration: 500,
                  transformOrigin: "50% 50%",
                  timingFunction: 'ease',
            })
            this.animation1 = animation1;
            // this.animation1 = animation;
            //   var id1 = address1.provinces[0].id1
            //   this.setData({
            //     provinces1: address1.provinces,
            //     citys1: address1.citys[id1],
            //     areas1: address1.areas[address1.citys[id][0].id1],
            //   })
            var id = address.provinces[0].id
            //     var id1 = address.provinces1[0].id
            this.setData({
                  provinces: address.provinces,

                  citys: address.citys[id],

                  areas: address.areas[address.citys[id][0].id],

            })
            that = this;
            //判断是不是选择了模板
            if (options.active == 1) {

                
                  var fhprovice = options.fhprovice;
                        if (fhprovice == 'undefined') {
                                  fhprovice=''
                            }
                  var fhcity = options.fhcity;
                  if (fhcity == 'undefined') {
                        fhcity = ''
                  }
                  var fharea = options.fharea;
                  if (fharea == 'undefined') {
                        fharea = ''
                  }
                  var shprovice = options.shprovice;
                  if (shprovice == 'undefined') {
                        shprovice = ''
                  }
                  var shcity = options.shcity;
                  if (shcity == 'undefined') {
                        shcity = ''
                  }
                  var sharea = options.sharea;
                  if (sharea == 'undefined') {
                        sharea = ''
                  }
                  //   货物
                  var goodsinfo = options.goodsinfo;
                  if (goodsinfo == 'undefined') {
                        goodsinfo = ''
                  }
                  //   重量
                  var dun = options.weight;
                  if (dun == 'undefined') {
                        dun = ''
                  }
                  //   占地
                  var fang = options.volume;
                  if (fang == 'undefined') {
                        fang = ''
                  }
                  //   金额
                  var freight = options.freight;
                  if (freight == 'undefined') {
                        freight = ''
                  }
                  //   个数
                  var jian = options.packnumber;
                  if (jian == 'undefined') {
                        jian = ''
                  }
                  //预付款
                  var yfk = options.yfk;
                  if (yfk == 'undefined') {
                        yfk = ''
                  }
                  //回单押金
                  var hdyj = options.hdyj;
                  if (hdyj == 'undefined') {
                        hdyj = ''
                  }
                       
                        console.log(options.volume + ';la;a;a;a;a')
                  this.setData({

                        name: options.nickname,
                        cph: options.carnumber,
                        phone_sj: options.mobile,

                        fhprovice: fhprovice,
                        fhcity: fhcity,
                        fharea: fharea,


                        shprovice: shprovice,
                        shcity: shcity,
                        sharea: sharea,
                        //   货物
                        goodsinfo: goodsinfo,
                        //   重量
                        dun: dun,
                        //   占地
                        fang: fang,
                        //   金额
                        freight: freight,
                        //   个数
                        jian: jian,
                        //预付款
                        yfk: yfk,
                        //回单押金
                        hdyj: hdyj,
                  });

            } else {
                  that.setData({
                        name: options.nickname,
                        cph: options.carnumber,
                        phone_sj: options.mobile,
                        fhprovice: '',
                        fhcity: '',
                        fharea: '',


                        shprovice: '',
                        shcity: '',
                        sharea: '',
                        //   货物
                        goodsinfo: '',
                        //   重量
                        dun: '',
                        //   占地
                        fang: '',
                        //   金额
                        freight: '',
                        //   个数
                        jian: '',
                        //预付款
                        yfk: '',
                        //回单押金
                        hdyj: '',
                  });
                  console.log(this.data.fhprovice + '==================');

            }


            var date = new Date()
            console.log("司机姓名：" + options.nickname + " 车牌号：" + options.carnumber + "手机号：" + options.mobile);




            var dates = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
            var times = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();

            this.setData({
                  dates,
                  time: times
            });


      },
      // ------------------- 分割线 --------------------
      onReady: function () {

      },

      bindDateChange: function (e) {
            this.setData({
                  dates: e.detail.value,
            });
      },



      //页面滑至底部事件
      onReachBottom: function () {
            // Do something when page reach bottom.
      },
      submitorders: function (e) {
     
            console.log(e)
            console.log("货物信息："+that.data.goodsinfo);
            //判断总运费>=预付款+回单押金
            console.log("总运费==" + that.data.freight);
            var f = parseFloat(that.data.freight);
            var _yfk = parseFloat(that.data.yfk);
            var _hdyj = parseFloat(that.data.hdyj);
            if(_hdyj+_yfk>f){
              wx.showToast({
                title: '运费填写有误！',
                icon: 'success',
                duration: 2500
              })
              return;

            }
          
            console.log("----货物信息未填写！！！--" + that.data.goodsinfo);
            if (that.data.goodsinfo.length==0){
              wx.showToast({
                title: '货物信息未填写！！！！',
                icon: 'success',
                duration: 2500
              })
              console.log("----货物信息未填写！！！--" + that.data.googinfo);
             return;
           }
            var userinfos = wx.getStorageSync('userinfos');
            var mobile = userinfos.mobile;
            console.log("----个人中心--" + mobile);
            //   var appkey = userinfos.appkey;
            console.log("----个人中心--" + appkey);
            console.log("提交订单:........");
            //     console.log("司机姓名:"+that.data.name);
            //     console.log("司机车牌号:"+ that.data.cph);
            //     console.log("电话号码：" + that.data.phone_sj);

            //     var temptime = that.data.dates + " " + that.data.time+":00";

            var shipdate = this.data.dates + ' ' + this.data.time;
            //     console.log("发货时间" + shipdate);
            //     console.log("目的地：" + that.data.shadress);
            //     console.log("始发地:" + that.data.fhadress);
            // if (that.data.areaInfo !== null || that.data.areaInfo !== undefined || that.data.areaInfo !== '') {
            //   console.log("------始发地为空");
            //  return;
            // } 

            // if (that.data.mudidi !== null || that.data.mudidi !== undefined || that.data.mudidi !== '') {
            //   console.log("------目的地为空");
            //   return;
            // } 
            // console.log("货物名称:" + that.data.googinfo);
            // if (that.data.googinfo !== null || that.data.googinfo!='') {
            //   console.log("------货物名称为空");
            //   return;
            // }
            // console.log("货物数量:" + that.data.dun + "吨" + that.data.fang + "方" +       that.data.jian+"件");
            // // if (that.data.dun! == null || that.data.jian!=null  || that.data.fang!== null) {
            // //   console.log("------货物数量信息不全");
            // //   return;
            // // }
            // console.log("总运费：" + that.data.zyf);
            // if (that.data.zyf!== null) {
            //   console.log("------总运费为空");
            //   return;
            // }
            // console.log("预付款：" + that.data.yfk);
            // if (that.data.yfk!== null) {
            //   console.log("------预付款为空");
            //   return;
            // }
            //     console.log("回单押金：" + that.data.hdyj);
            // if (that.data.hdyj! == null) {
            //   console.log("------回单押金为空");
            //   return;
            // }
            var fhaddress;
         
            var shaddress ;

            if (this.data.fhaddress ==''){
                  fhaddress = e.target.dataset.fhprovice+' '+e.target.dataset.fhcity+' '+e.target.dataset.fharea;
            }else{
                  fhaddress = this.data.fhaddress;
            }
            console.log("-------发货地址-----" + this.data.fhaddress == '');
            console.log("------ddddddd---" + fhaddress.length);
            if (fhaddress == "" || fhaddress.length ==2 ){
              wx.showToast({
                title: '始发地未填写！！！！',
                icon: 'success',
                duration: 2500
              })
              console.log("----始发地未填写！！！--" + fhaddress);
              return;
            }
          
           
            if (this.data.shaddress == '') {
                  shaddress = e.target.dataset.shprovice+' '+e.target.dataset.shcity +' '+ e.target.dataset.sharea;
            } else {
                  shaddress = this.data.shaddress;
            }
            console.log("-------目的地-----" + this.data.shaddress);
            if (this.data.shaddresss =="") {
              wx.showToast({
                title: '目的地未填写！！！！',
                icon: 'success',
                duration: 2500
              })
              console.log("----始发地未填写！！！--" + that.data.googinfo);
              return;
            }
            // packnumber: that.data.jian,
            //   weight: that.data.dun,
            //     volume: that.data.fang,
            if(this.data.fang==""){
              wx.showToast({
                title: '货物信息未填写！！！！',
                icon: 'success',
                duration: 2500
              })
              return;
            }
            console.log(shaddress + '-------------------------------')
            console.log(that.data.yfk + '-----------------------------预付款--')
            console.log(that.data.hdyj + '-----------------------------回单押金--')
            console.log(that.data.freight + '-----------------------------价格--')
            var myDate = new Date();

            var thistime = myDate.getTime();

            var appkey = app.globalData.appkey;

            var appsecret = app.globalData.appsecret;


            var str = mobile + appsecret + thistime;

            console.log('----------------' + str)


            var appsign = utilMd5.md5(str);

            appsign = appsign.replace("+", "%2B");

            appsign = appsign.replace("=", "%2A");


            console.log(appsign + '-----------------------------appsign')
            wx.showToast({
              title: '正在加载中.......',
              icon: 'loading'
              
            })
            wx.request({
                  url: getApp().globalData.url,
                  method: 'POST',
                  header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                  },
                  data: Util.json2Form({

                        packnumber: that.data.jian,
                        weight: that.data.dun,
                        volume: that.data.fang,
                        fhadress: fhaddress,
                        shadress: shaddress,
                        shipdate: shipdate,
                        mobile: mobile,
                        appkey: appkey,
                        viewtype: 4,
                        phone: that.data.phone_sj,
                        carNumber: that.data.cph,
                        goodsinfo: that.data.goodsinfo,
                        yfk: that.data.yfk,
                        hdyj: that.data.hdyj,
                        freight: that.data.freight,
                        appsign: appsign,
                        date: thistime,
                  }),
                 
                  success: function (res) {
                        console.log(res.data);
                        if (res.data.success) {
                          wx.hideLoading();//隐藏进度加载提示
                              wx.showToast({
                                    title: '创建成功',
                                    icon: 'success',
                                    duration: 2500
                              })
                              setTimeout(function () {
                                    wx.navigateBack()
                              }, 1200

                              )
                                    ;
                        }
                        else {
                              // wx.showToast({
                              //   title: 'error',
                              //   duration:2000
                              // })
                              wx.hideLoading();
                              wx.showToast({
                                    title: '创建订单失败了',
                                    icon: 'success',
                                    duration: 2500
                              })



                              console.log("---创建订单失败了-");
                        }
                  }
            })

      },
})







